package com.lqian.demo01;

import java.util.concurrent.*;

/**
 * 创建线程的5种方式
 * // 本质：5种方式都是new Thread().star();
 * */
public class HowToCreateThread {
    // public class Thread implements Runnable
    // 1 extends Thread
    static class MyThread extends Thread{
        @Override
        public void run() {System.out.println("Hello MyThread！");}
    }
    // 2 implements Runnable
    static class MyRun implements Runnable{
        @Override
        public void run() {System.out.println("Hello MyThread！");}
    }

    //带返回值的线程
    static class MyCall implements Callable<String>{
        @Override
        public String call() throws Exception {
            System.out.println("Hello MyThread！");
            return "success";
        }
    }

    public static void main(String[] args) throws Exception{
        new MyThread().start();
        new Thread(new MyRun()).start();
        // 3 lamda
        new Thread(()->{
            System.out.println("Hello MyThread！");
        }).start();
        //使用线程池
        // 4 ThreadPool
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(()->{
            System.out.println("Hello MyThread！");
        });
        //
        // 5 Callable 和Future、FutureTask
        //FutureTask 将来会产生返回值的任务
        // Thread中只能装Runnable接口 FutureTask实现了Runnable，Callable接口
        FutureTask<String> task = new FutureTask<>(new MyCall());
        Thread t = new Thread(task);
        t.start();
        System.out.println(task.get());

        //Future Callable 使用线程池
        //Future 异步的概念 不需要等待Callable执行完 Callable执行完之后会放到Future中
        Future<String> future = service.submit(new MyCall());
        String s = future.get();
        System.out.println(s);

        service.shutdown();

        String a="123"+new String("456");

        String s1 = new String("123");


    }
}
